package com.yayohos.locationmanager.base;

import com.yayohos.locationmanager.LocationManager;
import com.yayohos.locationmanager.configuration.LocationConfiguration;
import com.yayohos.locationmanager.listener.LocationListener;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

/**
 * Service中定位
 */
public abstract class LocationBaseService extends Ability implements LocationListener {
    private LocationManager locationManager;

    /**
     * 获取LocationConfiguration，用户自定义配置LocationConfiguration
     *
     * @return LocationConfiguration
     */
    public abstract LocationConfiguration getLocationConfiguration();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    public void onBackground() {
        super.onBackground();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        locationManager = new LocationManager.Builder(getApplicationContext())
                .configuration(getLocationConfiguration())
                .notify(this)
                .build();
        super.onCommand(intent, restart, startId);
    }

    /**
     * 获取LocationManager
     *
     * @return locationManager
     */
    protected LocationManager getLocationManager() {
        return locationManager;
    }

    /**
     * 定位开始
     */
    protected void getLocation() {
        if (locationManager != null) {
            locationManager.get();
        } else {
            throw new IllegalStateException("locationManager is null. "
                    + "Make sure you call super.onStartCommand before attempting to getLocation");
        }
    }


    @Override
    public void onDisconnect(Intent intent) {
        locationManager.onStop();
    }

    @Override
    public void onProcessTypeChanged(int processType) {
    }


    @Override
    public void onPermissionGranted(boolean alreadyHadPermission) {
    }

    @Override
    public void onStatusChanged(int status) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }
}